package com.zzyl.common.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * AI模型调用
 * @author: ZHD
 */
@Component
public class AIModelInvoker {

    @Autowired
    private  BaiDuAiProperties baiDuAiProperties;

    /**
     * 调用百度千帆大模型
     * @param prompt 提示词
     * @return  结果
     */
    public String qianfanInvoker(String prompt) {

        // 使用安全认证AK/SK鉴权，替换下列示例中参数，安全认证Access Key替换your_iam_ak，Secret Key替换your_iam_sk，如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
        Qianfan qianfan = new Qianfan(baiDuAiProperties.getAccessKey(), baiDuAiProperties.getSecretKey());

        // 指定模型
        ChatResponse resp = qianfan.chatCompletion()
                .model(baiDuAiProperties.getQianfanModel())
                .temperature(0.8)
                .maxOutputTokens(2048)
                .responseFormat("json_object")
                .addMessage("user", prompt)
                .execute();
        return resp.getResult();
    }
}